#include "MahjongTiles.h"

MahjongTiles::MahjongTiles()
{
	m_sValue = nullptr;
}

MahjongTiles::~MahjongTiles()
{
}

MahjongTiles* MahjongTiles::createWithTilesType(MAHJONG_TILES_MODE mode, MAHJONG_TILES_TYPE type, float ScaleDegress)
{
	MahjongTiles *pRet = new MahjongTiles();
		if (pRet && pRet->initWithTilesType(mode, type, ScaleDegress))
		{
		pRet->autorelease();
		return pRet;
		}
		else
		{
		delete pRet;
		pRet = NULL;
		return NULL;
		} 
}

bool MahjongTiles::initWithTilesType(MAHJONG_TILES_MODE mode, MAHJONG_TILES_TYPE type, float ScaleDegress)
{
	if (!Sprite::init())
	{
		return false;
	}


	m_ScaleDegree = ScaleDegress;
	m_Type = type;
	m_Mode = mode;
	setAnchorPoint(Vec2::ZERO);


	switch (m_Mode)
	{
	case MAHJONG_SELF_HAND:
		{

			this->setSpriteFrame("mj_zheng.png");

			m_sValue = Sprite::createWithSpriteFrameName(getTilesNameByType(type));
			m_sValue->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
			m_sValue->setPosition(this->getContentSize().width /2, this->getContentSize().height * 0.38);
			this->addChild(m_sValue, 10);
		}
		break;
	case MAHJONG_SELF_BRIGHT:
		{
			this->setSpriteFrame("mj_ping.png");

			m_sValue = Sprite::createWithSpriteFrameName(getTilesNameByType(type));
			m_sValue->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
			m_sValue->setPosition(this->getContentSize().width /2, this->getContentSize().height * 0.62);
			this->addChild(m_sValue, 10);

		}

		break;
	case MAHJONG_OUT:
		{
			setAnchorPoint(Vec2::ANCHOR_TOP_RIGHT);

			this->setSpriteFrame("mj_ping.png");
			m_sValue = Sprite::createWithSpriteFrameName(getTilesNameByType(type));
			m_sValue->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
			m_sValue->setPosition(this->getContentSize().width /2, this->getContentSize().height * 0.80);
			this->addChild(m_sValue, 10);

		}
		break;

	case MAHJONG_NORTH_HAND:
		{
			setAnchorPoint(Vec2::ANCHOR_TOP_RIGHT);
			this->setSpriteFrame("mj_bei.png");
		}
		break;

	case MAHJONG_EAST_HAND:
		{
			setAnchorPoint(Vec2::ZERO);
			this->setSpriteFrame("mj_east_bg.png");
		}
		break;

	case MAHJONG_EAST_BRIGHT:
		{
			setAnchorPoint(Vec2::ZERO);
			this->setSpriteFrame("mj_zheng_left.png");
		}
		break;

	case MAHJONG_WEST_HAND:
		{
			setAnchorPoint(Vec2::ZERO);
			this->setSpriteFrame("mj_west_bg.png");
		}
		break;

	case MAHJONG_WEST_BRIGHT:
		{
			setAnchorPoint(Vec2::ZERO);
			this->setSpriteFrame("mj_zheng_right.png");
		}
		break;


	case MAHJONG_DESK_NORTH:
		{
			setAnchorPoint(Vec2::ZERO);
			this->setSpriteFrame("mj_desk_bg.png");
		}
		break;

	case MAHJONG_DESK_SOUTH:
		{
			setAnchorPoint(Vec2::ZERO);
			this->setSpriteFrame("mj_desk_bg.png");
		}
		break;

	case MAHJONG_DESK_EAST:
		{
			setAnchorPoint(Vec2::ZERO);
			this->setSpriteFrame("mj_desk_right.png");
		}
		break;


	case MAHJONG_DESK_WEST:
		{
			setAnchorPoint(Vec2::ZERO);
			this->setSpriteFrame("mj_desk_left.png");
		}
		break;


	default:
		break;
	}

	m_lNum = Label::create("", "15", 20);
	m_lNum->setColor(Color3B::RED);
	m_lNum->enableOutline(Color4B::WHITE,5);

	this->setScale(m_ScaleDegree);
	this->addChild(m_lNum);


	return true;
}

void MahjongTiles::updateValue(MAHJONG_TILES_TYPE type)
{
	m_Type = type;
	if (m_sValue)
	{
		m_sValue->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(getTilesNameByType(type)));
	}
	else
	{
		log("error........tiles is nullptr, no value will display");
	}
}


void MahjongTiles::updateType( MAHJONG_TILES_TYPE type, MAHJONG_TILES_MODE mode, float ScaleDegress)
{
	if (m_sValue)
	{
		this->removeChild(m_sValue,true);
		m_sValue = nullptr;
	}

	if (ScaleDegress == -1.0)
	{
		ScaleDegress = m_ScaleDegree;
	}

	if (mode == MAHJONG_NOTHING)
	{
		mode = m_Mode;
	}
	initWithTilesType(mode , type, ScaleDegress);
}


const std::string MahjongTiles::getTilesNameByType(MAHJONG_TILES_TYPE type)
{
	std::string name;
	
	std::int8_t t = type;
	switch (t & 0xf0)
	{
	case 0x0:
		name = StringUtils::format("mj_wan_%d.png", t & 0xf);

		break;
	case 0x10:
		name = StringUtils::format("mj_tiao_%d.png", t & 0xf);

		break;
	case 0x20:
		name = StringUtils::format("mj_bing_%d.png", t & 0xf);

		break;
	case 0x30:
		name = StringUtils::format("mj_feng_%d.png", t & 0xf);

		break;
	case 0x40:
		name = StringUtils::format("mj_hua_%d.png", t & 0xf);

		break;

	default:
		name = StringUtils::format("mj_wan_%d.png", t & 0xf);

		break;
	}

	log("tiles name = %s", name.c_str());
	return name;
}

void MahjongTiles::setTilesSelected(bool isSelect /*= true*/)
{

// 	if (m_isSelected != isSelect)
// 	{
// 		if (m_isSelected)
// 		{
// 
// 			this->runAction(MoveBy::create(0.2, Vec2(0, )))
// 		}
// 		else
// 		{
// 
// 		}
// 	}
// 
// 	m_isSelected = isSelect;

}

void MahjongTiles::showNum(std::int16_t num)
{
	m_lNum->setString(StringUtils::format("%d", num).c_str());
}
